package samfeng.erp.warehouse.controller.admin.warehouse;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import samfeng.erp.warehouse.controller.vo.*;
import samfeng.erp.warehouse.convert.WarehouseConvert;
import samfeng.erp.warehouse.dal.dataobject.WarehouseDO;
import samfeng.erp.warehouse.dal.mysql.WarehouseMapper;
import samfeng.erp.warehouse.service.WarehouseService;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Api(tags = "管理后台 - 仓库")
@RestController
@RequestMapping("/warehouse")
@Validated
@Slf4j
public class WarehouseController {

    @Resource
    private WarehouseService warehouseService;

    @Resource
    private WarehouseMapper warehouseMapper;

    @PostMapping("/create")
    @ApiOperation("创建仓库")
    @PreAuthorize("@ss.hasPermission('stock:warehouse:create')")
    public CommonResult<Long> createWarehouse(@Valid @RequestBody WarehouseCreateReqVO createReqVO) {
        return success(warehouseService.createWarehouse(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新仓库")
    @PreAuthorize("@ss.hasPermission('stock:warehouse:update')")
    public CommonResult<Boolean> updateWarehouse(@Valid @RequestBody WarehouseUpdateReqVO updateReqVO) {
        warehouseService.updateWarehouse(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除仓库")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('stock:warehouse:delete')")
    public CommonResult<Boolean> deleteWarehouse(@RequestParam("id") Long id) {
        warehouseService.deleteWarehouse(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得仓库")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('stock:warehouse:query')")
    public CommonResult<WarehouseRespVO> getWarehouse(@RequestParam("id") Long id) {
        WarehouseDO warehouse = warehouseService.getWarehouse(id);
        return success(WarehouseConvert.INSTANCE.convert(warehouse));
    }

    @GetMapping("/list")
    @ApiOperation("获得仓库列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('stock:warehouse:query')")
    public CommonResult<List<WarehouseRespVO>> getWarehouseList(@RequestParam("ids") Collection<Long> ids) {
        List<WarehouseDO> list = warehouseService.getWarehouseList(ids);
        return success(WarehouseConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得仓库分页")
    @PreAuthorize("@ss.hasPermission('stock:warehouse:query')")
    public CommonResult<PageResult<WarehouseRespVO>> getWarehousePage(@Valid WarehousePageReqVO pageVO) {
        PageResult<WarehouseDO> pageResult = warehouseService.getWarehousePage(pageVO);
        return success(WarehouseConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出仓库 Excel")
    @PreAuthorize("@ss.hasPermission('stock:warehouse:export')")
    @OperateLog(type = EXPORT)
    public void exportWarehouseExcel(@Valid WarehouseExportReqVO exportReqVO,
                                     HttpServletResponse response) throws IOException {
        List<WarehouseDO> list = warehouseService.getWarehouseList(exportReqVO);
        // 导出 Excel
        List<WarehouseExcelVO> datas = WarehouseConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "仓库.xls", "数据", WarehouseExcelVO.class, datas);
    }

    @GetMapping("/getSelectList")
    @ApiOperation("获得仓库列表")
    @PreAuthorize("@ss.hasPermission('stock:warehouse:query')")
    public CommonResult<List<WarehouseRespVO>> getWarehouseList(@RequestParam("id") Long id) {
        List<WarehouseDO> stockWarehouses;
        if (id == 0) {
            stockWarehouses = warehouseMapper.selectList();
        } else {
            stockWarehouses = warehouseMapper.selectList("id", id);
        }
        return success(WarehouseConvert.INSTANCE.convertList(stockWarehouses));
    }

}
